﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using KillBill.Business.Entities;
using KillBill.Presentation.Common.WindowsForms.View;
using KillBill.Presentation.Modules.Sales.IView;
using KillBill.Presentation.Modules.Sales.Presenter;

namespace KillBill.Presentation.Modules.Sales.View
{
    public partial class BillingView : KillBillWinFormView, IBilling
    {
        public BillingView()
        {
            InitializeComponent();
            Presenter = new BillingPresenter(this);
        }

        #region IBilling Members

        public BillingPresenter Presenter { get; set; }
        public string UserId
        {
            get { return txbxId.Text; }
            set { txbxId.Text = value; }
        }
        public string UserName
        {
            get { return txbxName.Text; }
            set { txbxName.Text = value; }
        }
        public string UserAddress
        {
            get { return txbxAddress.Text; }
            set { txbxAddress.Text = value; }
        }
        public string UserPhone
        {
            get { return txbxPhone.Text; }
            set { txbxPhone.Text = value; }
        }

        public string ProductSearchValue
        {
            get
            {
                return txbxSearchProduct.Text ;
            }
            set
            {
                txbxSearchProduct.Text = value;
            }
        }

        public int DetailCuantity
        {
            get
            {
                return (int)numUpCuantity.Value;
            }
            set
            {
                numUpCuantity.Value = value;
            }
        }

        public decimal SubTotal
        {
            get
            {
                return Convert.ToDecimal(txbxSubTotal.Text);
            }
            set
            {
                txbxSubTotal.Text = value.ToString("#.00#");
            }
        }

        public decimal Iva
        {
            get
            {
                return Convert.ToDecimal(txbxIVA.Text);
            }
            set
            {
                txbxIVA.Text = value.ToString("#.00#");
            }
        }

        public decimal Ice
        {
            get
            {
                return Convert.ToDecimal(txbxICE.Text);
            }
            set
            {
                txbxICE.Text = value.ToString("#.00#");
            }
        }

        public decimal Total
        {
            get
            {
                return Convert.ToDecimal(txbxTotal.Text);
            }
            set
            {
                txbxTotal.Text = value.ToString("#.00#");
            }
        }

        private List<Product> _products;
        public List<Product> Products
        {
            get
            {
                return _products;
            }
            set
            {
                _products = value;
                #region ControlMapping
                gviewProducts.AutoGenerateColumns = false;

                var codeColumn = new DataGridViewTextBoxColumn();
                codeColumn.DataPropertyName = "ProductID";
                codeColumn.HeaderText = "CODIGO";

                var nameColumn = new DataGridViewTextBoxColumn();
                nameColumn.DataPropertyName = "Name";
                nameColumn.HeaderText = "NOMBRE";

                var priceColumn = new DataGridViewTextBoxColumn();
                priceColumn.DataPropertyName = "LastPrice";
                priceColumn.HeaderText = "PRECIO";
                gviewProducts.Columns.Clear();

                gviewProducts.Columns.Add(codeColumn);
                gviewProducts.Columns.Add(nameColumn);
                gviewProducts.Columns.Add(priceColumn);

                gviewProducts.DataSource = value;
                #endregion
            }
        }

        private Dictionary<string,Detail> _detail = new Dictionary<string,Detail>();
        public Dictionary<string, Detail> Detail
        {
            get
            {
                return _detail;
            }
            set
            {
                _detail = value;
                this.listViewDetail.Items.Clear();
                var auxD=0;
                foreach (var detail in _detail)
                {
                    listViewDetail.Items.Add(detail.Key);
                    listViewDetail.Items[auxD].SubItems.Add(detail.Value.Quantity.ToString());
                    listViewDetail.Items[auxD].SubItems.Add(detail.Value.Product.Name);
                    listViewDetail.Items[auxD].SubItems.Add(detail.Value.Product.LastPrice.ToString("#.00#"));
                    listViewDetail.Items[auxD].SubItems.Add(detail.Value.TotalValue.ToString("#.00#"));
                    auxD++;
                }
            }
        }

        public Product SelectedObject { get; set; }

        #endregion
        private void TxbxIdLeave(object sender, EventArgs e)
        {
            Presenter.GetCustomerData();
        }
        private void BtnAddDetailClick(object sender, EventArgs e)
        {
            if (gviewProducts.SelectedRows.Count != 1) return;
            SelectedObject = Products[gviewProducts.SelectedRows[0].Index];
            Presenter.AddProductToDetail();
        }

        private void BtnLoadProductsClick(object sender, EventArgs e)
        {
            Presenter.GetProducts();
        }

        private void BtnAceptClick(object sender, EventArgs e)
        {
            Presenter.SaveData();
        }

        private void BtnEditCustomerClick(object sender, EventArgs e)
        {
            Presenter.EditCustomer();
        }
        private void BtnCancelClick(object sender, EventArgs e)
        {
            Presenter.ClearData();
        }
    }
}
